<extend name="_layout/main"/>
<block name="head">
    <style>
        .mid {
            text-align: center;
            font-weight: bold;
        }
        .shopcar .list .right .fu-tit{
            color:#da242b;
        }
    </style>
</block>
<block name="top">
    <header class="mui-bar mui-bar-nav" id="header">
        <!--<a class="btn" href="javascript:history.go(-1)">-->
        <!--<i class="iconfont icon-fanhui"></i>-->
        <!--</a>-->
        <div class="top-sch-box top-sch-boxtwo flex-col mid">
            购物车
        </div>
        <!--<a class="btn" href="#">-->
        <!--<i class="iconfont icon-lajixiang"></i>-->
        <!--</a>-->
    </header>
    <!-- 侧边导航 -->
</block>
<block name="content">
    <div class="warp warptwo clearfloat">
        <form name="cartForm" action="{:U('Cart/order')}">
            <div class="shopcar clearfloat">
                <volist name="results" id="result">
                    <div class="list clearfloat fl">
                        <div class="xuan clearfloat fl">
                            <div class="radio">
                                <label>
                                    <input type="checkbox" name="cids[]" <lt name="result.goods_store" value="1">disabled</lt> value="{$result.id}"/>
                                    <div class="option"></div>
                                </label>
                            </div>
                        </div>

                        <div class="tu clearfloat fl">
                            <a href="{:U('Goods/detail', ['id'=>$result['goods_id']])}">
                                <span></span>
                                <img src="{$result.goods_thumb}"/>
                            </a>
                        </div>
                        <div class="right clearfloat fl">
                            <a href="{:U('Goods/detail', ['id'=>$result['goods_id']])}">
                                <p class="tit over">{$result.goods_name}</p>
                                <p class="fu-tit over">
                                    <gt name="result.pay_balance" value="0">
                                        {$result.pay_balance}现金
                                    </gt>
                                    <if condition="$result['pay_balance'] gt 0 && $result['pay_integral'] gt 0">
                                        +
                                    </if>
                                    <gt name="result.pay_integral" value="0">
                                        {$result.pay_integral}积分
                                    </gt>
                                    <if condition="$result['pay_coupon'] gt 0 && $result['pay_integral'] gt 0">
                                        +
                                    </if>
                                    <gt name="result.pay_coupon" value="0">
                                        {$result.pay_coupon}券
                                    </gt>
                                </p>
                                <p class="jifen over">库存：{$result.goods_store}</p>
                            </a>
                            <div class="bottom clearfloat">
                                <div class="zuo clearfloat fl">
                                    <ul data-id="{$result.id}">
                                        <li><img src="__STATIC__/img/jian.png" data-num="{$result.goods_number}"
                                                 class="btn-decrement"/></li>
                                        <li id="number_box_{$result.id}">{$result.goods_number}</li>
                                        <li><img src="__STATIC__/img/jia.png" class="btn-increment"/></li>
                                    </ul>
                                </div>
                                <i class="iconfont icon-lajixiang fr" data-id="{$result.id}"></i>
                            </div>
                        </div>

                    </div>
                </volist>
            </div>
        </form>
    </div>

    <!--settlement star-->
    <div class="settlement clearfloat">
        <div class="zuo clearfloat fl box-s">
            合：<span>现金{$totalBalance} ，积分{$totalIntegral}，券{$totalCoupon}</span>
        </div>
        <a href="javascript:void(0)" class="fl db" id="btn-take-order">
            立即结算
        </a>
    </div>
    <!--settlement end-->
    <include file="_layout/footer"/>
</block>
<block name="scripts">
    <script>
        $(document).on('click', '.btn-decrement', function (e) {
            var _id = $(this).parent().parent().data('id');
            var _num = $(this).data('num');
            if (parseInt(_num) < 1) {
                mui.toast('商品数量最少为1');
                return;
            }
            if (!isNaN(_id)) {
                $.post("{:U('Cart/decrement_goods_number')}", {id: _id}, function (xdata) {
                    if (xdata.status == 'success') {
                        $('#number_box_' + _id).html(xdata.data.goods_number);
                    } else {
                        mui.toast(xdata.message);
                    }
                }, 'json');
            }
        })

        $(document).on('click', '.btn-increment', function (e) {
            var _id = $(this).parent().parent().data('id');
            if (!isNaN(_id)) {
                $.post("{:U('Cart/increment_goods_number')}", {id: _id}, function (xdata) {
                    if (xdata.status == 'success') {
                        $('#number_box_' + _id).html(xdata.data.goods_number);
                    } else {
                        mui.toast(xdata.message);
                    }
                }, 'json');
            }
        })

        $(document).on('click', '.icon-lajixiang', function (e) {
            var _this = $(this);
            var _id = _this.data('id');
            if (isNaN(_id)) {
                return;
            }
            mui.confirm('确认删除吗？', '警告', ['确定', '取消'], function (btn) {
                if (btn.index == 0) {
                    $.get("{:U('Cart/delete')}", {ids: [_id]}, function (xdata) {
                        if (xdata.status == 'success') {
                            mui.toast(xdata.message, '提示');
                            _this.parent().parent().parent().remove();
                        } else {
                            mui.alert(xdata.message, '错误');
                        }
                    }, 'json');
                }
            })

        })
        $(document).on('click', '#btn-take-order', function (e) {
            var form = $("form[name='cartForm']");
            if (!form.is('form')) {
                return;
            }

            var action = form.attr('action');
            if (!action || action == '') {
                return;
            }

            var data = form.serialize();
            mui.confirm('确认结算以上商品吗？系统将会自动从您的账户扣除相应的现金/积分/商品券', '友情提示', ['确定', '取消'], function (btn) {
                if (btn.index == 0) {
                    $.post(action, data, function (xdata) {
                        if (xdata.status == 'success' && xdata.data.url) {
                            mui.alert(xdata.message, '提示', ['确定'], function () {
                                document.location.href = xdata.data.url;
                            });
                        }else{
                            mui.toast(xdata.message)
                        }
                    }, 'json');
                }
            })

        })
    </script>
</block>